package com.contactsconverter.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.contactsconverter.model.CTContactModel;
import com.contactsconverter.model.CTNumberModel;
import com.contactsconverter.R;

public class CTContactAdapter extends ArrayAdapter<CTContactModel>{

	private Context mContext;
	private List<CTContactModel> mListOffContact;
	

	public CTContactAdapter(Context context, int resource,
			List<CTContactModel> objects) {
		super(context, resource, objects);
		this.mContext = context;
		this.mListOffContact = objects;
	}


	@Override
	public View getDropDownView(int position, View convertView,ViewGroup parent) {
		return getView(position, convertView, parent);
	}



	@Override
	public int getPosition(CTContactModel item) {
		int n = 0;
		for (CTContactModel contact : mListOffContact) {
			if(contact.getId() == item.getId())
				return n;
			n++;
		}
		return super.getPosition(item);
	}

	@Override
	public  View getView(int position, View convertView, ViewGroup parent)  {

		 final CTContactModel contact = mListOffContact.get(position);
		 
		LayoutInflater mInflater = (LayoutInflater)
				mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.contact_row, null);
			
			

		}
		
		LinearLayout linearL = (LinearLayout)convertView.findViewById(R.id.tn_contact_phone_number);
		linearL.removeAllViews();
		for (CTNumberModel ctNumber : contact.getNumbers()) {
			
			LinearLayout ll = (LinearLayout)mInflater.inflate(R.layout.number_layout, null);
			linearL.addView(ll);
			TextView tv = (TextView)ll.findViewById(R.id.ct_phone_number);
			TextView tvc = (TextView)ll.findViewById(R.id.ct_phone_number_converted);
			
			tv.setText(ctNumber.getNumber());
			
			if(ctNumber.getConverterNumber()!=null)
			{
				tvc.setText("vers  "+ctNumber.getConverterNumber());
			}
			
			ImageView iv = (ImageView)ll.findViewById(R.id.ct_phone_provider);
			iv.setBackgroundResource(ctNumber.getProvider());
		}
		
		TextView txtName = (TextView) convertView.findViewById(R.id.tn_contact_name);
		txtName.setText(contact.getName());
		
		TextView txtConverter= (TextView) convertView.findViewById(R.id.is_convert);
		
	
		if(contact.getConvertCode()==Integer.valueOf(getContext().getString(R.string.is_converted_int)).intValue())
		{
			txtConverter.setText(getContext().getString(R.string.is_converted));
		}
		else if(contact.getConvertCode()==Integer.valueOf(getContext().getString(R.string.notall_converted_int)).intValue())
		{
			txtConverter.setText(getContext().getString(R.string.notall_converted));
		}
		else if(contact.getConvertCode()==Integer.valueOf(getContext().getString(R.string.to_converted_int)).intValue()){
		
			txtConverter.setText(getContext().getString(R.string.to_converted));
		}
		else{
			txtConverter.setText(getContext().getString(R.string.notto_converted));
		}
		
		
		
		CheckBox check = (CheckBox ) convertView.findViewById(R.id.tn_contact_check);
		check.setChecked(contact.isSelected());
		
		
		check.setOnClickListener(new OnClickListener() {
			 
			  @Override
			  public void onClick(View v) {
		       		
				  contact.setSelected(!contact.isSelected());

			  }
		});
		
		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.tn_contact_user_avatar);
		imgIcon.setImageBitmap(getRoundedCornerBitmap(contact.getAvatar(), 20));
		
		
		//TextView ctPhoneNumber = (TextView) convertView.findViewById(R.id.ct_phone_number);
		//ctPhoneNumber.setText(contact.toString2());
		
		
		return convertView;
	}
	public List<CTContactModel> getmListOffContact() {
		return mListOffContact;
	}
	
	private Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xFF0084B4;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 255, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }


	

	
	


}
